#pragma once
#include <utility>
namespace lw
{
	enum Color
	{
		RED,
		BLACK
	};

	template<class T>
	struct RBTreeNode
	{
		RBTreeNode<T>* _left;
		RBTreeNode<T>* _right;
		RBTreeNode<T>* _parent;
		T _data;
		Color _col;
		RBTreeNode(const T& data)
			:_left(nullptr)
			,_right(nullptr)
			,_parent(nullptr)
			,_data(data)
			,_col(RED)
		{}

	};

	template<class T, class Ref, class Ptr>
	struct RBTreeIterator
	{
		typedef RBTreeNode<T> Node;
		typedef RBTreeIterator<T, Ref, Ptr> Self;
		Node* _node;
		RBTreeIterator(Node* node)
			:_node(node)
		{}

		Self& operator++()
		{
			Node* curr = _node;
			if (curr->_right)
			{
				Node* subR = curr->_right;
				while (subR->_left)
				{
					subR = subR->_left;
				}
				_node = subR;
			}
			else
			{
				Node* parent = curr->_parent;
				while (parent && parent->_right == curr)
				{
					curr = parent;
					parent = curr->_parent;
				}
				_node = parent;
			}
			return *this;
		}
		Self operator++(int)
		{
			Self tmp(*this);
			++* this;
			return tmp;
		}
		Self& operator--()
		{
			Node* curr = _node;
			if (curr->_left)
			{
				Node* subL = curr->_left;
				while (subL->_right)
				{
					subL = subL->_right;
				}
				_node = subL;
			}
			else
			{
				Node* parent = curr->_parent;
				while (parent && parent->_left == curr)
				{
					curr = parent;
					parent = curr->_parent;
				}
				_node = parent;
			}
			return *this;
		}
		Self operator--(int)
		{
			Self tmp(*this);
			--* this;
			return  tmp;
		}
		Ref operator*()
		{
			return _node->_data;
		}
		Ptr operator->()
		{
			return &(_node->_data);
		}
		bool operator!=(const Self& s) const
		{
			return _node != s._node;
		}
		bool operator==(const Self& s) const
		{
			return _node == s._node;
		}
	};

	template<class K, class T, class KeyOfT>
	class RBTree
	{
		typedef RBTreeNode<T> Node;

	public:
		typedef RBTreeIterator<T, T&, T*> iterator;
		typedef RBTreeIterator<T, const T&, const T*> const_iterator;

		iterator begin()
		{
			Node* curr = _root;
			while (curr && curr->_left)
			{
				curr = curr->_left;
			}
			return curr;
		}
		iterator end()
		{
			return nullptr;
		}
		const_iterator begin() const
		{
			Node* curr = _root;
			while (curr && curr->_left)
			{
				curr = curr->_left;
			}
			return curr;
		}
		const_iterator end() const
		{
			return nullptr;
		}
		pair<iterator, bool> Insert(const T& data)
		{
			if (_root == nullptr)
			{
				_root = new Node(data);
				_root->_col = BLACK;
				return make_pair(_root, true);
			}
			KeyOfT kot;
			Node* curr = _root;
			Node* parent = nullptr;
			while (curr)
			{
				if (kot(curr->_data) < kot(data))
				{
					parent = curr;
					curr = curr->_right;
				}
				else if (kot(curr->_data) > kot(data))
				{
					parent = curr;
					curr = curr->_left;
				}
				else
				{
					return make_pair(curr, false);
				}
			}
			curr = new Node(data);
			Node* newnode = curr;
			if (kot(parent->_data) < kot(data))
				parent->_right = curr;
			else
				parent->_left = curr;
			curr->_parent = parent;
			while (parent && parent->_col == RED)
			{
				Node* grandfather = parent->_parent;
				if (parent == grandfather->_left)
				{
					Node* uncle = grandfather->_right;
					if (uncle && uncle->_col == RED)
					{
						parent->_col = BLACK;
						uncle->_col = BLACK;
						grandfather->_col = RED;

						curr = grandfather;
						parent = curr->_parent;
					}
					else
					{
						if (curr == parent->_left)
						{
							//       g
							//   p       u
							//c
							RotatoR(grandfather);
							parent->_col = BLACK;
							grandfather->_col = RED;

						}
						else
						{
							//       g
							//   p       u
							//     c
							RotatoL(parent);
							RotatoR(grandfather);
							curr->_col = BLACK;
							grandfather->_col = RED;

						}
						break;
					}
				}
				else
				{
					Node* uncle = grandfather->_left;
					if (uncle && uncle->_col == RED)
					{
						parent->_col = BLACK;
						uncle->_col = BLACK;
						grandfather->_col = RED;

						curr = grandfather;
						parent = curr->_parent;
					}
					else
					{
						if (curr == parent->_right)
						{
							//       g
							//   u       p
							//             c
							RotatoL(grandfather);
							parent->_col = BLACK;
							grandfather->_col = RED;

						}
						else
						{
							//       g
							//   u      p
							//        c
							RotatoR(parent);
							RotatoL(grandfather);
							curr->_col = BLACK;
							grandfather->_col = RED;

						}
						break;
					}
				}
			}
			_root->_col = BLACK;
			return make_pair(newnode, true);
		}
		void RotatoL(Node* parent)
		{
			Node* subR = parent->_right;
			Node* subRL = subR->_left;
			parent->_right = subRL;
			if (subRL)
				subRL->_parent = parent;
			subR->_left = parent;
			Node* ppnode = parent->_parent;
			parent->_parent = subR;
			if (parent == _root)
			{
				_root = subR;
				subR->_parent = nullptr;
			}
			else
			{
				if (ppnode->_left == parent)
					ppnode->_left = subR;
				else
					ppnode->_right = subR;
				subR->_parent = ppnode;
			}

		}

		void RotatoR(Node* parent)
		{
			Node* subL = parent->_left;
			Node* subLR = subL->_right;
			parent->_left = subLR;
			if (subLR)
				subLR->_parent = parent;
			subL->_right = parent;
			Node* ppnode = parent->_parent;
			parent->_parent = subL;
			if (parent == _root)
			{
				_root = subL;
				subL->_parent = nullptr;
			}
			else
			{
				if (ppnode->_left == parent)
					ppnode->_left = subL;
				else
					ppnode->_right = subL;
				subL->_parent = ppnode;
			}
		}
	private:
		Node* _root = nullptr;
	};
}